﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class PI : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument PurchaseInvoice;
        private ReportDocument PurchaseInvoiceDetail;
        private string Tipe;

        public PI()
        {
            DA = new DataAccess<object>();
            PurchaseInvoice = new ReportDocument();
            PurchaseInvoiceDetail = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            if (IsPostBack)
                if (RadioButtonList2.SelectedValue.ToString().Contains("Summary"))
                    LoadReportSummary(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
                else
                    LoadReportDetail(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (RadioButtonList2.SelectedValue.ToString().Contains("Summary"))
                LoadReportSummary(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
            else
                LoadReportDetail(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
        }


        public void LoadReportSummary(string  NoDO,string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(
                "Select  a.PINo,a.PIDate,a.Kurs,a.Term,a.[Description],a.Ppn,a.CurrencyCode,a.Discount, ");
            sb.AppendFormat("CASE WHEN a.DiscountType IS NULL THEN '%' ELSE a.DiscountType END AS DiscountType ");
            sb.Append(",i.SerialNumber,i.NoBundle,i.QtyPack, ");
            sb.AppendFormat("i.Panjang,i.Lebar,i.Tinggi,i.QtyUnit as Qty,i.KodeMasuk,i.KodePallet,i.SatuanPack,i.SatuanUnit,");
            sb.AppendFormat("c.*,it.ItemName,s.SubGroupName,iw.KodeWarehouse, ");
            sb.AppendFormat("b.Price,b.Discount AS DiscountD,b.DiscountType AS DiscountTypeD,c.SupplierName ");
            sb.AppendFormat("from PurchaseInvoice a  ");
            sb.AppendFormat("inner Join PurchaseInvoiceDetail b on a.PINo = b.PINo  ");
            sb.AppendFormat("LEFT JOIN inventory i ON i.SerialNumber = b.SerialNumber  ");
            sb.AppendFormat("Left Join Supplier c on c.SupplierId = a.SupplierId   ");
            sb.AppendFormat("Left Join Item it on it.ItemId = i.ItemId   ");
            sb.AppendFormat("Left Join Subgroup s on s.SubGroupID = it.SubgroupId ");
            sb.AppendFormat("Left JOIN uvw_mutasi_stok iw ON i.SerialNumber = iw.SerialNumber ");
            sb.AppendFormat("Where a.PINo='{0}'  Order By s.SubgroupId,i.ItemId  ", NoDO);


            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/PurchaseInvoice.rpt");
            PurchaseInvoice.Load(reportPath);
            PurchaseInvoice.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            PurchaseInvoice.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = PurchaseInvoice;
        }

        public void LoadReportDetail(string NoDO,string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(
                "Select a.PINo,a.PIDate,a.Kurs,a.Term,a.[Description],a.Ppn,a.CurrencyCode,a.Discount, ");
            sb.AppendFormat("CASE WHEN a.DiscountType IS NULL THEN '%' ELSE a.DiscountType END AS DiscountType ");
            sb.Append(",i.SerialNumber,i.NoBundle,i.QtyPack, ");
            sb.AppendFormat("i.Panjang,i.Lebar,i.Tinggi,i.QtyUnit as Qty,i.KodeMasuk,i.KodePallet,i.SatuanPack,i.SatuanUnit,");
            sb.AppendFormat("c.*,it.ItemName,s.SubGroupName,iw.KodeWarehouse, ");
            sb.AppendFormat("b.Qty,b.Price,b.Discount AS DiscountD,b.DiscountType AS DiscountTypeD,c.SupplierName ");
            sb.AppendFormat("from PurchaseInvoice a  ");
            sb.AppendFormat("inner Join PurchaseInvoiceDetail b on a.PINo = b.PINo  ");
            sb.AppendFormat("LEFT JOIN inventory i ON i.SerialNumber = b.SerialNumber  ");
            sb.AppendFormat("Left Join Supplier c on c.SupplierId = a.SupplierId   ");
            sb.AppendFormat("Left Join Item it on it.ItemId = i.ItemId   ");
            sb.AppendFormat("Left Join Subgroup s on s.SubGroupID = it.SubgroupId ");
            sb.AppendFormat("Left JOIN uvw_mutasi_stok iw ON i.SerialNumber = iw.SerialNumber ");
            sb.AppendFormat("Where a.PINo='{0}'  Order By s.SubgroupId,i.ItemId  ", NoDO);

          
            var Source = DA.GetDataListDataSet(sb.ToString());
            string reportPath = Server.MapPath("~/Report/PurchaseInvoiceDetail.rpt");
            PurchaseInvoiceDetail.Load(reportPath);
            PurchaseInvoiceDetail.SetDataSource(Source.Tables[0]);
            var param = new ParameterDiscreteValue();
            param.Value = tipe;
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            PurchaseInvoiceDetail.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
            CrystalReportViewer1.ReportSource = PurchaseInvoiceDetail;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            PurchaseInvoice.Clone();
            PurchaseInvoice.Dispose();

            PurchaseInvoiceDetail.Clone();
            PurchaseInvoiceDetail.Dispose();

        }
      
    }
}